這篇我們實作PlayerChessmanMoveData中剩下的方法
我們判斷國王與城堡之間的格子為空白格,且國王與城堡皆為位移動過的狀態時,返回可易位的格子
public ArrayList getArrCastlingMoveData(HashMap<String,Object> chessboardData, int position,HashMap<String,Object> castlingData){
ArrayList canCastlingList = new ArrayList<>();
if(player.equals("1")){
for(int i=0;i<64;i++){
if(i == 60 && i == position){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e1").equals(true)
&& chessboardData.get(playerChessboardData.getStringBoardData(56)).equals(own+"R") && castlingData.get("a1").equals(true)){
if(chessboardData.get(playerChessboardData.getStringBoardData(57)).equals(" ")
&& chessboardData.get(playerChessboardData.getStringBoardData(58)).equals(" ")
&& chessboardData.get(playerChessboardData.getStringBoardData(59)).equals(" ")){
canCastlingList.add(58);
}
}
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e1").equals(true)
&& chessboardData.get(playerChessboardData.getStringBoardData(63)).equals(own+"R") && castlingData.get("h1").equals(true)){
if(chessboardData.get(playerChessboardData.getStringBoardData(61)).equals(" ")
&& chessboardData.get(playerChessboardData.getStringBoardData(62)).equals(" ")){
canCastlingList.add(62);
}
}
}
}
}
else if (player.equals("2")) {
for(int i=0;i<64;i++){
if(i == 59 && i == position){
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e8").equals(true)
&& chessboardData.get(playerChessboardData.getStringBoardData(63)).equals(own+"R") && castlingData.get("a8").equals(true)){
if(chessboardData.get(playerChessboardData.getStringBoardData(61)).equals(" ")
&& chessboardData.get(playerChessboardData.getStringBoardData(62)).equals(" ")
&& chessboardData.get(playerChessboardData.getStringBoardData(60)).equals(" ")){
canCastlingList.add(61);
}
}
if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e8").equals(true)
&& chessboardData.get(playerChessboardData.getStringBoardData(56)).equals(own+"R") && castlingData.get("h8").equals(true)){
if(chessboardData.get(playerChessboardData.getStringBoardData(57)).equals(" ")
&& chessboardData.get(playerChessboardData.getStringBoardData(58)).equals(" ")){
canCastlingList.add(57);
}
}
}
}
}
return canCastlingList;
}
將在初始位置的士兵返回可走2格的走法
public ArrayList getArrSpMoveData(int position){
ArrayList canSpMoveList = new ArrayList<>();
for(int i=0;i<64;i++){
if(i >= 48 && i <= 55 && position == i){
canSpMoveList.add(i - 16);
}
}
return canSpMoveList;
}
吃過路兵的規則較複雜,我們利用士兵狀態的陣列(pawnMove)來判斷該士兵是否為第一次行走2格,若是則產生可以吃棋的走法
public ArrayList getArrEpMoveData(HashMap<String,Object> chessboardData, HashMap<String,Object> pawnMove1,HashMap<String,Object> pawnMove2, int position){
int up = position/8;
int left = position%8;
int down = 7-position/8;
int right = 7-position%8;
ArrayList canEpMoveList = new ArrayList<>();
if(player.equals("1")){
if(position >= 24 && position <= 31){
if(left == 0 && right != 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
&& pawnMove2.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
canEpMoveList.add(position-7);
}
}else if(left != 0 && right == 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
&& pawnMove2.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
canEpMoveList.add(position-9);
}
}else if(left !=0 && right != 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
&& pawnMove2.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
canEpMoveList.add(position-9);
}
if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
&& pawnMove2.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
canEpMoveList.add(position-7);
}
}
}
}
else if(player.equals("2")){
if(position >= 24 && position <= 31){
if(left == 0 && right != 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
&& pawnMove1.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
canEpMoveList.add(position-7);
}
}else if(left != 0 && right == 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
&& pawnMove1.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
canEpMoveList.add(position-9);
}
}else if(left !=0 && right != 0){
if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
&& pawnMove1.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
canEpMoveList.add(position-9);
}
if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
&& pawnMove1.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
canEpMoveList.add(position-7);
}
}
}
}
return canEpMoveList;
}
這樣我們便完成了所有棋子行走的方法,下篇我們來實作判斷棋子到指定格中間是否有棋子阻擋